// 1. painless pipeline 仿真, 这里不支持 """ 这种, kibana devtool支持
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "split": {
          "field": "tags",
          "separator": ","
        }
      },
      {
        "script": {
          // "source": " if(ctx.containsKey('content')){ ctx.content_length = ctx.content.length();}else{ctx.content_length=0;} "
          "source": " ctx.content_length = ctx.containsKey('content') ? ctx.content.length() : 0; "
        }
      },
      {
        "set":{
          "field": "views",
          "value": 0
        }
      }
    ]
  },

  "docs": [
    {
      "_index":"index",
      "_id":"id",
      "_source":{
        "title":"Introducing big data......",
        "tags":"hadoop,elasticsearch,spark",
        "content":"You konw, for big data"
      }
    },


    {
      "_index":"index",
      "_id":"idxx",
      "_source":{
        "title":"Introducing cloud computering",
        "tags":"openstack,k8s",
        "content":"You konw, for cloud"
      }
    }

    ]
}

// 2. 更新
// 添加测试数据
delete test_blogs

put test_blogs/_doc/1
{
    "title": "Introducing big data......",
    "tags": "hadoop,elasticsearch,spark",
    "content": "You konw, for big data",
    "views": 0
}

put test_blogs/_doc/2
{
    "title": "Introducing db ......",
    "tags": "mysql, postgresql, es",
    "content": "You konw, for data",
    "views": 0
}

get test_blogs/_search

// 使用painless 来更新数据
POST test_blogs/_update/1
{
  "script": {
    "source": "ctx._source.views += params.new_views",
    "params": {
      "new_views": 100
    }
  }
}

// 查看views
GET test_blogs/_doc/1

// 把这个脚本保存下来
POST _scripts/update_views
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.views += params.new_views"
  }
}

// 使用保存的脚本来更新views
POST test_blogs/_update/1
{
  "script": {
    "id": "update_views",
    "params": {
      "new_views": 1
    }
  }
}

// 查看views
GET test_blogs/_doc/1

// 3. search
get test_blogs/_search

get test_blogs/_search
{
  "script_fields": {
    "rnd_views": {
      "script": {
        "lang": "painless",
        "source": " java.util.Random rnd = new Random(); doc['views'].value + rnd.nextInt(1000)"
      }
    }
  }
}
